/*
 * Copyright (c) 2011 Auzingers
 * Created by Oui-IT
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.auzingers.docsorganizer.googleapi;

import com.google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager;

import android.accounts.Account;
import android.content.Context;

/** Helper singleton for access to Google APIs. */
public final class GoogleUtil {

    private static GoogleUtil instance = null;

    //private Context ctx;

    private GoogleAccountManager accountManager;

    private GoogleDocsAndroidRequestInitializer requestInitializer;

    private DocsClient dc;

    private String accountName;

    private GooglePreferencesUtil preferences;

    /** Reference to api for calls to Google. */
    private GoogleAPI api;

    /**
     * Private constructor of singleton.
     * @param ctx context - usually this is the application context
     */
    private GoogleUtil(Context ctx) {
        //this.ctx = ctx;
        accountManager = new GoogleAccountManager(ctx);
        preferences = GooglePreferencesUtil.getInstance(ctx.getApplicationContext());
        requestInitializer = new GoogleDocsAndroidRequestInitializer(ctx, accountManager);
        dc = new DocsClient(requestInitializer.createRequestFactory());
        api = new GoogleAPI(dc);
        dc.setPrettyPrint(true);
        dc.setApplicationName("Google-DocsSample/1.0");
    }

    /**
     * Returns the singleton instance - creates a new one if it does not exist.
     * @param ctx context - usually this is the application context
     * @return singleton instance of GoogleUtil
     */
    public static synchronized GoogleUtil getInstance(Context ctx) {
        if (instance == null) {
            instance = new GoogleUtil(ctx);
        }
        return instance;
    }

    public GoogleAccountManager getAccountManager() {
        return accountManager;
    }

    /**
     * Stores the authentication token.
     *
     * @param authToken token to store
     */
    public void setAuthToken(String authToken) {
        preferences.setAuthToken(authToken);
        requestInitializer.setAuthToken(authToken);
    }

    /**
     * Stores the account name.
     *
     * @param accountName account name to store
     */
    public void setAccountName(String accountName) {
        preferences.setAccountNameAndRemoveSessionId(accountName);
        this.accountName = accountName;
        requestInitializer.setGsessionid(null);
    }

    public Account getCurrentAccount() {
        return accountManager.getAccountByName(accountName);
    }

    public String getInitializerAuthToken() {
        return requestInitializer.getAuthToken();
    }

    public GoogleAPI getAPI() {
        return api;
    }

}
